package com.habo.test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.habo.dao.ShareMapper;
import com.habo.dao.UserMapper;
import com.habo.entity.Share;
import com.habo.entity.TestEntity;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

/**
 * Created by junbb on 2017/3/29.
 */
public class Excuseme {
    @Test
    public void testA() throws IOException {
        Gson gson = new Gson();
        int i = gson.fromJson("100", int.class);                // 100
        double d = gson.fromJson("\"99.99\"", double.class);    // 99.99
        boolean b = gson.fromJson("true", boolean.class);       // true
        String str = gson.fromJson("String", String.class);     // String
        System.out.println(i + " " + d + " " + b + " " + str);

        String jsonNumber = gson.toJson(100);       // 100
        String jsonBoolean = gson.toJson(false);    // false
        String jsonString = gson.toJson("String");  // "String"
        System.out.println(jsonNumber + " " + jsonBoolean + " " + jsonString);


        TestEntity testEntity = new TestEntity("chan", 10, 20.3);
        String jsonObject = gson.toJson(testEntity);    // {"name":"chan","age":10,"price":20.3}
        System.out.println(jsonObject);

        String jsonStrings = "{\"name\":\"chan\",\"age\":10,\"price\":20.3}";
        TestEntity testEntitys = gson.fromJson(jsonStrings, TestEntity.class);  // TestEntity {name='chan', age=10, price=20.3}
        System.out.println(testEntitys);

        TestEntity testEntityA = new TestEntity();
        JsonReader jsonReader = new JsonReader(new StringReader(jsonStrings));
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String s = jsonReader.nextName();
            switch (s) {
                case "name":
                    testEntityA.setName(jsonReader.nextString());
                    break;
                case "age":
                    testEntityA.setAge(jsonReader.nextInt());
                    break;
                case "price":
                    testEntityA.setPrice(jsonReader.nextDouble());
                    break;
            }
        }
        jsonReader.endObject();
        System.out.println(testEntityA);
        gson.toJson(testEntityA, System.out);
        /**
         Gson gson = new GsonBuilder()
         //序列化null
         .serializeNulls()
         // 设置日期时间格式，另有2个重载方法
         // 在序列化和反序化时均生效
         .setDateFormat("yyyy-MM-dd")
         // 禁此序列化内部类
         .disableInnerClassSerialization()
         //生成不可执行的Json（多了 )]}' 这4个字符）
         .generateNonExecutableJson()
         //禁止转义html标签
         .disableHtmlEscaping()
         //格式化输出
         .setPrettyPrinting()
         .create();
         */
    }

    @Test
    public void TestB() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
        ShareMapper shareMapper = (ShareMapper) applicationContext.getBean("shareMapper");
        Date date = new Date();
        System.out.println(date);
        Share share = new Share("001", "test_contentB", "test_httpB", date);
//        shareMapper.shareInsert(share);
//        List<Share> list = shareMapper.shareAllByPage(001, 0, 2);
        List<Share> lists = shareMapper.shareFindById(001);
        for (Share s :
                lists) {
            System.out.println(s);
        }
    }

    @Test
    public void TestC() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");
        for (String key :
                map.keySet()) {
            System.out.println(key + " " + map.get(key));
        }
        String str = "abc";

        List<TestEntity> testEntities = new ArrayList<>();
        TestEntity a = new TestEntity("aa", 1, 1.01);
        TestEntity b = new TestEntity("bb", 2, 2.01);
        testEntities.add(a);
        testEntities.add(b);
        for (TestEntity t :
                testEntities) {
            System.out.println(t);
        }
    }

    @Test
    public void TestD() {
        String str = "fuck you manhttp://v.youku.com/v_show/id_XMjY4ODM2MjkzMg==.html?f=49422135&spm=a2hww.20023042.m_223465.5~5~5~5!2~5~5~A&from=y1.3-idx-beta-1519-23042.223465.4-1";
        System.out.println(str.indexOf("id_"));
        System.out.println(str.substring(0, str.indexOf("http:")));
        System.out.println(str.substring(str.indexOf("http:")));
        System.out.println(str.contains("http:"));
    }
}
